package com.qianfeng.tulingdemo;

import android.os.AsyncTask;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created by Administrator on 2016/11/12.
 */
public class JsonAsyncTask extends AsyncTask<String,Void,String>{
    private String url="http://www.tuling123.com/openapi/api?key=080390332ce0704a54cd5a3aaf70f443&info=";
     //定义消息回来的接口
   private OnMessageResult mResult;

    public JsonAsyncTask(OnMessageResult result) {
        mResult = result;
    }

    @Override
    protected String doInBackground(String... strings) {
        try {
            String message= URLEncoder.encode(strings[0],"utf-8");
            url=url+message;
            byte[]data=HttpURLConnHelper.loadByteFromURL(url);
            JSONObject jsonObject=new JSONObject(new String(data));
            return jsonObject.optString("text");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return "";
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        if (s != null) {
            mResult.SendStringData(s);
        }

    }
}
